function [F,H,Hraw,Q,R,ystar,Tstar,x0,Sig0,z,D,A,a] = MatricesKalman3_Juan_Extended(phi,delta,Sigma_epsilon,Sigma_trend,lambda,...
    rho,Sigma_v,y,trend,exog,n,nftot,k,nnonmonthly,nmonthly,p_final,VarTypeInd)
%MatricesKalman This function maps the DFM parameters into State Space
    %Matrices F,H,Q,R
    
%     if isempty(rho), rho = zeros(n,1); end
    
    % Transition Matrix F
       
     phi2 = [];                                                             % Add extra columns to the matrix Phi, which is the
     for lag = 1:p_final                                                    % matrix of VAR coefficients of the true factors,
        jj = (lag-1)*1+1;                                                  % to accomodate for the trend components
        phi2b = [zeros(1,trend) phi(:,jj:jj+1-1)];
        phi2 = [phi2 phi2b];
     end
     phi2 = [zeros(trend,size(phi2,2)); phi2];
     if trend >0
     phi2(1:trend,1:trend) = eye(trend);
     end
    
     nf2 = 1+trend;
     
     F1a = [];                                                              % Add extra columns to the matrix Phi, which is the
     for lag = 1:p_final                                                    % matrix of VAR coefficients of the true factors,
        jj = (lag-1)*nf2+1;                                                  % to accomodate for the trend and the idiosyncratic components
        F1b = [phi2(:,jj:jj+nf2-1) zeros(nf2,nnonmonthly)];
        F1a = [F1a F1b];
     end
     
     F1c = [];
     for lag = 1:2
       temp = diag(rho(find(VarTypeInd>100)',lag));
       F1c = [F1c zeros(nnonmonthly,nf2) temp];
     end
     
     F1 = [F1a; F1c zeros(nnonmonthly,k-size(F1c,2));];
     F2 = [eye(nftot*(p_final-1)) zeros(nftot*(p_final-1),nftot)];
 
     F = [F1; F2];           % Final Transition Matrix F    
    
   % Loading Matrix H

       H = zeros(nnonmonthly+nmonthly,k);
       H1 = LambdaToH_3_Extended(lambda,VarTypeInd, nnonmonthly);
       H(1:size(H1,1),1:size(H1,2)) = H1;
       
       % Adjust for Soft Variab;es
          
       
        % Adjust H for serial correlation
            
        ystar = y;
        Tstar = size(y,1);
        
        Hraw = H;
            

            rho_m = rho(VarTypeInd<100,:);
            Hraw1 = H((VarTypeInd<100),:);
            [finalMatrix] = MatrixPolynomial(p_final,nmonthly,2,nftot,rho_m,Hraw1);
            
            H(VarTypeInd<100,:) = finalMatrix;

            for i = find(VarTypeInd<100)'
                serieslag = mlag2(y(:,i),2);
                ystar(:,i) = y(:,i)+((-rho(i,:))*serieslag')';
            end

            ystar = ystar(2+1:end,:);

            Tstar = size(ystar,1);

       
    
    % State Covariance Matrix Q
        
            
            Q = zeros(k,k,Tstar);
            
               if trend > 0
                Q(1:trend,1:trend,:) = repmat(Sigma_trend,1,1,Tstar);               
               end
               
                   
                   Q(trend+1:trend+1,trend+1:trend+1,:) = Sigma_epsilon;
                   
               
               
               positions=find(VarTypeInd>=100);
               
                       
                   for j=1:size(positions,1)
                       
                       jj=positions(j);
                       Q(trend+1+j,trend+1+j,:) = Sigma_v(jj,:);
                       
                   end
                   

            R = repmat(1E-6*eye(n),1,1,Tstar);
            for j = 1:n
                if ismember(j,find(VarTypeInd<100))
                    R(j,j,:) = Sigma_v(j,:);
                end
            end
        
    
    % Initial State Mean and Covariance Matrices

        x0 = zeros(k,1);        % Initialization of the state vector
        Sig0 = 1*eye(k);      % for the Kalman Filer
    
    % Regressor Matrices z and A

        z = ones(Tstar,1);
        A = zeros(n,1);
        
    % Exogenous variables in State Vector
    
        if ~isempty(exog)
        D = delta;
        a = exog(2+1:end,:);
        else
        D = zeros(k,1);
        a = [];
        end
    
    

end

